/*
 * MobileGPS.java
 *
 * Created on March 11, 2008, 4:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package uns.fit.gis.gps;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import java.io.*;
import henson.midp.Float;

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import net.benhui.btgallery.*;
import net.benhui.btgallery.bluelet.*;


/**
 *
 * @author Hoang Hai
 */
public class MobileGPS {
	
    public static Preferences sPreferences;
    public static Gps sGps;
    private static Track sTrack;
    private static AstroInformation sAstroInformation;
    private static Timer sTimer;
    public static BLUElet mBluelet; //Ban moi - NLNN
    
    public static void searchGPS()
    {
        mBluelet.startInquiry(DiscoveryAgent.GIAC, new UUID[]{new UUID(0x1101)});
    }
    
    public static void connectGPS(int kSelected)
    {
        mBluelet.selectDevice(kSelected);
    }
    
    public static void getGPSInformation()
    {
        ServiceRecord serviceRecord = mBluelet.getFirstDiscoveredService();
        RemoteDevice remoteDevice = mBluelet.getSelectedDevice();
        String url = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
        String host = null;
        try {
            host = remoteDevice.getFriendlyName(false);
        }
        catch (IOException ex) {
            System.out.println("Can not connect to Bluetooth Device!");
        }
        Preferences preferences = getPreferences();
        preferences.setBluetoothHost(host);
        preferences.setBluetoothUrl(url);
        preferences.setConnectType(Preferences.CONNECTTYPE_BLUETOOTH);
        preferences.save();
    }

    public static void stop() {
            sTrack.close();
            sGps.close();
    }
    
    
    public static void init()
    {
        sPreferences = new Preferences();
        sGps = new Gps();
        sTrack = new Track();
        sAstroInformation = new AstroInformation();
        mBluelet = new BLUElet();
        sTimer = new Timer();
    }
        
    public class RefreshTimerTask extends TimerTask {
        public void run() {
        }
    }
    
    public static Preferences getPreferences() {
        return sPreferences;
    }
    
    public static Gps getGps() {
        return sGps;
    }
    
    public static Track getTrack() {
        return sTrack;
    }
    
    public static AstroInformation getAstroInformation() {
        return sAstroInformation;
    }
    
    public static boolean fileCapable() {
        return System.getProperty("microedition.io.file.FileConnection.version") != null;
    }

    public static boolean serialCapable() {
        return System.getProperty("microedition.commports") != null;
    }
    
    public static boolean midp2Capable() {
        return System.getProperty("microedition.profiles").equals("MIDP-2.0");
    }
    
    public static boolean bluetoothCapable() {
        try {
            Class.forName("javax.bluetooth.LocalDevice");
            return true;
        } catch (ClassNotFoundException ex) {
            return false;
        }
    }
    
    public static boolean obexCapable() {
        try {
            Class.forName("javax.obex.ClientSession");
            return true;
        } catch (ClassNotFoundException ex) {
            return false;
        }
    }
    
    public static String floatToString(Float f, int decimals) {
        StringBuffer sb = new StringBuffer();
        sb.append(Float.Int(f).toLong());
        if (decimals > 0) {
            sb.append('.');
            int i;
            Float n = Float.abs(f);
            for (i = 0; i < decimals; i++) {
                n = Float.Frac(n).Mul(10);
                sb.append(Float.Int(n).toLong());
            }
        }
        return sb.toString();
    }

    public static String getDateString(long milliseconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getDefault());
        calendar.setTime(new Date(milliseconds));
        return calendar.get(Calendar.YEAR) + "-" + twoDigits(calendar.get(Calendar.MONTH) + 1) + "-" +  twoDigits(calendar.get(Calendar.DAY_OF_MONTH));
    }
    
    private static String twoDigits(int n) {
        return (n < 10 ? "0" : "") + n;
    }

    public static String getDateString() {
        return getDateString(System.currentTimeMillis());
    }
    
}
